home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE18 / EX1.C next >
C/C++ Source or Header  |  1994-12-27  |  4KB  |  102 lines

  1. #include <genstub.c>
  2.  
  3.       LRESULT CALLBACK WndProc (HWND  hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  4.       {
  5.          static HWND hNextViewer ;       /* Next viewer in the clipboard viewer chain */
  6.          
  7.          switch (uMsg)  /* process windows messages */
  8.          {   
  9.             case WM_CREATE:    /* make this application a clipboard viewer */
  10.                     hNextViewer = SetClipboardViewer( hWnd ); 
  11.                     SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)"My Clipboard Viewer");
  12.             break;
  13.  
  14.             // right button mouse interface
  15.  
  16. #include <rbutton.c>
  17.  
  18.             case WM_PAINT: {
  19.  
  20.                     PAINTSTRUCT ps;             //  paint params from BeginPaint
  21.                     HBITMAP     hBitmap;        //  handle to bitmap on clipboard
  22.                     RECT        rClientRect;    //  area to write in
  23.                     HANDLE      hMem;           //  handle to text on clipboard
  24.  
  25.                     BeginPaint( hWnd, &ps );
  26.                     GetClientRect( hWnd, &rClientRect );
  27.                     OpenClipboard( hWnd );
  28.                     if (hMem = GetClipboardData( CF_TEXT )) 
  29.                     { 
  30.                        /* try to get data as CF_TEXT format first */
  31.                        LPSTR lpMem  = GlobalLock( hMem );
  32.                        DrawText( ps.hdc, lpMem, -1, &rClientRect, DT_LEFT );
  33.                        GlobalUnlock( hMem );
  34.                     }
  35.                     else if (hBitmap = GetClipboardData( CF_BITMAP )) 
  36.                     { /* if there is no text, try CF_BITMAP format */
  37.                        BITMAP      bm;
  38.                        HDC hMemDC = CreateCompatibleDC( ps.hdc );
  39.                        SelectObject( hMemDC, hBitmap );
  40.                        GetObject( hBitmap, sizeof (BITMAP), (LPSTR) &bm );
  41.                        BitBlt( ps.hdc, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY );
  42.                        DeleteDC( hMemDC );
  43.                     }
  44.                     CloseClipboard( );
  45.                     EndPaint( hWnd, &ps );
  46.                  }     
  47.                  break ;
  48.  
  49.             case WM_DRAWCLIPBOARD:
  50.                     if (hNextViewer)/* relay message to next viewer */
  51.                        SendMessage( hNextViewer, WM_DRAWCLIPBOARD,wParam, lParam );
  52.                     InvalidateRect( hWnd, NULL, TRUE );    /* Force repaint of own window*/
  53.             break;
  54.  
  55.             case WM_CHANGECBCHAIN:
  56.                     // Process this message to update hNextViewer static 
  57.                     // if the next clipboard viewer is going away.
  58.                     if (wParam == hNextViewer)
  59.                        hNextViewer = LOWORD(lParam);
  60.                     else if (hNextViewer) /* relay message to next viewer */
  61.                        SendMessage( hNextViewer, WM_CHANGECBCHAIN, wParam, lParam );
  62.                     break;
  63.  
  64.             case WM_COMMAND:   /* process menu items */
  65.                     switch (LOWORD(wParam)) {
  66.                        case IDM_TEST:   
  67.                           /* empty the clipboard */
  68.                           OpenClipboard( hWnd );
  69.                           EmptyClipboard( );
  70.                           CloseClipboard( );
  71.                           /* Force repaint. */
  72.                           InvalidateRect( hWnd, NULL, TRUE );
  73.                        break ;
  74.                        case IDM_EXIT:
  75.                           DestroyWindow( hWnd );
  76.                        break;
  77. #include <aboutopt.c>
  78.                     }
  79.                     break;
  80.  
  81.             case WM_DESTROY:   /* stop application */
  82.                     if (hNextViewer && !ChangeClipboardChain( hWnd, hNextViewer ))
  83.                     {
  84.                        // Serious problem, most likely a bug in the detection of next chain change.
  85.                        char szErrMsg[128];
  86.                        wsprintf( szErrMsg, "Clipboard chain corrupt. Err %d detected. Next viewer: %x", 
  87.                                  GetLastError(), hNextViewer );
  88.                        MessageBox( hWnd, szErrMsg, "Fatal System Error", MB_OK );
  89.                     }
  90.                     PostQuitMessage( 0 );
  91.                     break ;
  92.  
  93.             default:
  94.                /* default windows message processing */
  95.                     return DefWindowProc( hWnd, uMsg, wParam, lParam);
  96.          }
  97.          return (0L) ;
  98.       }
  99.  
  100. #include <about.c>
  101.  
  102.